<?php

namespace App\Admin\Controllers;

use App\Admin\Controllers\Helpers\LinkHelper;
use App\Models\GroupMember;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GroupMemberController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GroupMember(), function (Grid $grid) {
            $grid->model()->with(['group', 'leader', 'member']);
            $grid->column('leader_id')
                ->display(function () {
                    return LinkHelper::displayUser($this->leader);
                });
            $grid->column('user_id')
                ->display(function () {
                    return LinkHelper::displayUser($this->member);
                });
            $grid->column('joined_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('leader_id');
            });

            $grid->disableBatchDelete();
            $grid->disableBatchActions();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GroupMember(), function (Show $show) {
            $show->field('id');
            $show->field('group_id');
            $show->field('leader_id');
            $show->field('user_id');
            $show->field('joined_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GroupMember(), function (Form $form) {
            $form->display('id');
            $form->text('group_id');
            $form->text('leader_id');
            $form->text('user_id');
            $form->text('joined_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
